<template>
	<view>
		<form>
			<view class="cu-form-group">
				<view class="title">原密码</view>
				<input placeholder="请输入原登录密码" type="password" name="input" v-model="oldPassword"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">新密码</view>
				<input placeholder="请输入新登录密码" type="password" name="input" v-model="password"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">确认密码</view>
				<input placeholder="请再次输入新登录密码" type="password" name="input" v-model="password1"></input>
			</view>
		</form>
		<div class="common-btn">
			<button class="cu-btn block bg-green margin-tb-sm lg" @click="submit">确认修改</button>
		</div>
		
	</view>
</template>

<script>
	import api from '@/api/api.js';
	export default {
		data() {
			return {
				oldPassword:undefined,
				password:undefined,
				password1:undefined
			}
		},
		methods: {
			
			submit(){
				if (this.utils.isEmpty(this.oldPassword)) {
					uni.showToast({
						title: "请输入旧密码",
						icon: 'none',
					});
					return
				}
				if (this.utils.isEmpty(this.password)) {
					uni.showToast({
						title: "请输入新密码",
						icon: 'none',
					});
					return
				}
				if (this.password!=this.password1) {
					uni.showToast({
						title: "两次密码输入不正确",
						icon: 'none',
					});
					return
				}
				console.log(this.password);
				var patrn=/^(\w){6,20}$/;  
				if (!patrn.exec(this.password)) {
					uni.showToast({
						title: "密码只能输入6-20个字母、数字、下划线",
						icon: 'none',
					});
					return
				}
				
				var data = {oldPassword:this.oldPassword,password:this.password};
				api.setPwd(data).then(
					res => {
						if(res.code==200){
							uni.showToast({
							    title: '密码修改成功',
							    duration: 2000
							});
							setTimeout(function() {
								uni.navigateBack({
								  success: () => {
								  }
								})
							}, 500);
						}
						else{
							uni.showToast({
								title: res.msg,
								icon: 'none',
							});
						}
						
					},
					rej => {}
				);
				
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
